package com.itheima.server.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.server.interceptor.UserHolder;
import com.itheima.tanhua.dubbo.api.BlackListApi;
import com.itheima.tanhua.dubbo.api.QuestionApi;
import com.itheima.tanhua.dubbo.api.SettingsApi;
import com.itheima.tanhua.model.vo.SettingsVo;
import com.itheima.tanhua.model.domain.PageResult;
import com.itheima.tanhua.model.domain.Question;
import com.itheima.tanhua.model.domain.Settings;
import com.itheima.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private BlackListApi blackListApi;

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    /**
     * 查看通用设置
     * @return
     */
    public SettingsVo settings() {
        SettingsVo settingsVo = new SettingsVo();
        //获取用户id
        Long userId = UserHolder.getUserId();
        settingsVo.setId(userId);

        //获取手机号码
        String phone = UserHolder.getPhone();
        settingsVo.setPhone(phone);

        //获取陌生人问题
        Question question = questionApi.fingQuestionById(userId);
        String txt = question == null ? "java从入门到放弃" : question.getTxt();
        settingsVo.setStrangerQuestion(txt);

        //获取用户的APP通知开关数据
        Settings settings = settingsApi.fingSettingsById(userId);
        if (settings != null){
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }

            return settingsVo;
    }

    /**
     * 设置陌生人问题
     * @param txt
     */
    public void saveQuestions(String txt) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //调用api查找当前的陌生人问题是否存在
        Question question = questionApi.fingQuestionById(userId);

        if (question == null){
            //不存在，保存
            Question q = new Question();
            q.setTxt(txt);
            q.setUserId(userId);
            questionApi.saveQuestion(q);
        }else {
            //存在修改
            question.setTxt(txt);
            questionApi.updateQusetion(question);
        }

    }



    /**
     * 黑名单
     * @param page
     * @param pageSize
     */
    public  PageResult blacklist(Integer page, Integer pageSize) {
        Long userId = UserHolder.getUserId();

        Page<UserInfo> userInfoPage = blackListApi.blackList(userId, page, pageSize);

        PageResult pageResult = new PageResult(page,pageSize, (int) userInfoPage.getTotal(),userInfoPage.getRecords());



        return pageResult;
    }

    /**
     * 通知设置
     * @param map 设置
     */
    public void saveSettings(Map map) {
        boolean likeNotification = (Boolean) map.get("likeNotification");
        boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");
        //1、获取当前用户id
        Long userId = UserHolder.getUserId();
        //2、根据用户id，查询用户的通知设置
        Settings settings = settingsApi.fingSettingsById(userId);
        //3、判断
        if(settings == null) {
            //保存
            settings = new Settings();
            settings.setUserId(userId);
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.save(settings);
        } else {
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.update(settings);
        }
    }
}
